//
//  MyImageView.m
//  makepack_tools
//
//  Created by tangs on 2018/8/22.
//  Copyright © 2018年 tangs. All rights reserved.
//

#import "MyImageView.h"

@implementation MyImageView

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
    // Drawing code here.
}

- (void)update {
    id item = self.data;
    NSRange range = [self.confingPath rangeOfString:@"/" options:NSBackwardsSearch];
    NSString *dir = [[NSString alloc] initWithFormat:@"resources/%@/", item[1]];
    if ([item count] > 12 && item[12] != nil && [item[12] length] > 0) {
        dir = [[NSString alloc] initWithFormat:@"resources/Icon/%@/", item[12]];
    }
    NSString *path = [[NSString alloc] initWithFormat:@"%@/%@/res/drawable-xxhdpi-v4/ic_launcher.png",
                      [self.confingPath substringToIndex:(range.location)],
                      dir];
    NSImage *image = [[NSImage alloc] initWithContentsOfFile:path];
    if (image) {
        self.image = image;
    } else {
        self.image = [NSImage imageNamed:NSImageNameCaution];
    }
}

- (void)onCellUpdated:(NSNotification *)notification {
    id item = [[notification userInfo] valueForKey:@"data"];
    if (item == self.data) {
        [self update];
    }
}

- (void)bindData:(NSMutableArray *)data withConfigPath:(NSString*)path {
    self.data = data;
    self.confingPath = path;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(onCellUpdated:)
                                                 name:@"update_cell"
                                               object:nil];
}

@end
